package com.pengtu.gsj.controller.yh;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.yh.MainTenance;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.yh.MainTenanceService;

@Controller
@RequestMapping("mainTenance")
public class MainTenanceController extends DefaultBaseController {
	
	@Autowired
	private MainTenanceService mainTenanceService;

	/**
	 * 
	 * load:
	 * 适用:每个请求执行前都会执行该方法，绑定特定对象供更新和删除使用
	 * @param id  对象id
	 * @return 业务数据对象
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public MainTenance load(String id) {
		MainTenance mainTenance;
		if (StringUtils.isNotEmpty(id)) {
			mainTenance = mainTenanceService.getMainTenanceById(id);
		}else {
			mainTenance = new MainTenance();
		}
		return mainTenance;
	}
	
	/**
	 * 
	 * showLisfInfo:
	 * 适用:跳转到显示大中修数据记录列表
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showLisfInfo() {
		return "yh/mainTenance_list";
	}
	
	/**
	 * 
	 * getMainTenanceList:
	 * 适用:jqgrid请求记录数据
	 * @param page
	 * @param request
	 * @return 返回记录及页面相关数据信息
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<MainTenance> getMainTenanceList(Page<MainTenance> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = mainTenanceService.findMainTenance(page, queryFilter);
		return page;
	}
	
	/**
	 * 
	 * saveMainTenance:
	 * 适用:保存及更新记录数据
	 * @param mainTenance
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public boolean saveMainTenance(@ModelAttribute MainTenance mainTenance) {
		mainTenanceService.saveMainTenance(mainTenance);
		return true;
	}
	
	/**
	 * 
	 * showMainTenance:
	 * 适用:ajax请求获得特定对象返回到页面显示
	 * @param mainTenance  
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="show")
	@ResponseBody                                                     
	public MainTenance showMainTenance(@ModelAttribute MainTenance mainTenance){
		return mainTenance;
	}
	
	/**
	 * 
	 * deleteMainTenance:
	 * 适用:ajax删除指定对象
	 * @param mainTenance 通过id获得的指定对象
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="delete")
	@ResponseBody
	public boolean deleteMainTenance(@ModelAttribute MainTenance mainTenance){
		mainTenanceService.deleteMainTenance(mainTenance);
		return true;
	}
}
